import dayjs from "dayjs";
import isBetween from "dayjs/plugin/isBetween";
import isoweek from "dayjs/plugin/isoWeek";
import "dayjs/locale/zh-cn"; // 导入中文语言包
// 设置 dayjs 使用中文 locale
dayjs.locale("zh-cn");
dayjs.extend(isoweek);
dayjs.extend(isBetween);
// 判断是否在当前日期的前一周和后一周范围内
export function isDateInRange(time) {
  const currentDate = dayjs();
  const oneWeekAgo = currentDate.subtract(7, "day");
  const oneWeekLater = currentDate.add(7, "day");
  return dayjs(time).isBetween(oneWeekAgo, oneWeekLater, null, "[]");
}

//禁用未来日期
export function getCurrentDate(time) {
  return dayjs(time).isAfter(dayjs(), "day");
}
// 获取当前日期往前数 N 天的时间区间
export const getPastDateRange = (days = 7) => {
  const currentDate = dayjs();
  const startDate = currentDate.subtract(days, "day");
  const formatDateString = (date) => date.format("YYYY-MM-DD");
  return [formatDateString(startDate), formatDateString(currentDate)];
};

/**
 * 分解日期为上午和下午
 * @param {*} dateRange
 * @returns
 */
export const decomposeDateRange = (dateRange) => {
  const [startDate, endDate] = dateRange;
  const start = dayjs(startDate);
  const end = dayjs(endDate);
  const days = [];

  for (
    let current = start;
    current.isSameOrBefore(end);
    current = current.add(1, "day")
  ) {
    const morning = current.startOf("day").format("YYYY-MM-DD 上午");
    const afternoon = current.endOf("day").format("YYYY-MM-DD 下午");
    days.push(morning, afternoon);
  }

  return days;
};
/**
 * 
 * @returns 获取当前日期
 
 */
export function getCurrentFormattedDate() {
  const currentTime = dayjs();

  if (currentTime.hour() < 12) {
    return currentTime.format("YYYY年MM月DD日 上午");
  } else {
    return currentTime.format("YYYY年MM月DD日 下午");
  }
}

// 获取本周的起始日期和结束日期以及本周是一年中的第几周
export function getWeekInfo(weekOffset, date) {
  const today = date ? dayjs(date) : dayjs();
  const startOfWeek = today
    .add(weekOffset, "week")
    .startOf("isoWeek")
    .format("YYYY-MM-DD");
  const endOfWeek = today
    .add(weekOffset, "week")
    .endOf("isoWeek")
    .format("YYYY-MM-DD");
  const weekNumber = today.add(weekOffset, "week").week();
  const year = today.add(weekOffset, "week").year();
  return {
    startOfWeek,
    endOfWeek,
    weekNumber,
    year
  };
}

/**
 * 获取当前月份
 * @returns 
 */
export function getCurrentMonth(date) {
  let day = date?dayjs(date):dayjs()
  return day.format('YYYY-MM');
}


/**
 * 获取选中日期的周一和后四周的周日的区间
 * @param {*} selectedDate 
 * @returns 
 */
export function getWeekRange(selectedDate) {
  const selectedDay = dayjs(selectedDate);
  const startOfWeek = selectedDay.startOf('week'); // 当前周的周一
  const endOfWeek = selectedDay.endOf('week'); // 当前周的周日
  const weekRanges = [];

  // 添加当前选中周的周一
  const startMonday = startOfWeek.day() === 1 ? startOfWeek : startOfWeek.add(1, 'day'); // 如果当前选中的是周一，则直接加一天
  weekRanges.push({
    start: startMonday.format('YYYY-MM-DD'),
    end: endOfWeek.format('YYYY-MM-DD')
  });

  // 计算后续五周的周日
  for (let i = 1; i <= 4; i++) {
    const nextSunday = endOfWeek.add(i, 'week').endOf('week'); // 下一周的周日
    weekRanges.push({
      start: nextSunday.startOf('week').format('YYYY-MM-DD'),
      end: nextSunday.format('YYYY-MM-DD')
    });
  }

  return weekRanges;
}


/**
 * 格式化时间
 * @param {*} time:需要格式化的时间
 * @param {*} format :格式化的格式  YYYY-MM-DD HH:mm:ss
 * @returns 格式化时间
 */
export function formatTime(time = null, format = "YYYY-MM-DD") {
  if (time) {
    return dayjs(time).format(format);
  } else {
    return dayjs().format(format);
  }
}
